package com.example.springcloud.service.impl;

import java.util.List;

import com.example.springcloud.domain.Path;
import com.example.springcloud.domain.Route;
import com.example.springcloud.mapper.PathMapper;
import com.example.springcloud.mapper.RouteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.springcloud.mapper.UserRouteFavoriteMapper;
import com.example.springcloud.domain.UserRouteFavorite;
import com.example.springcloud.service.IUserRouteFavoriteService;

/**
 * 用户路线收藏Service业务层处理
 * 
 * @author helian
 * @date 2025-03-09
 */
@Service
public class UserRouteFavoriteServiceImpl implements IUserRouteFavoriteService 
{
    @Autowired
    private UserRouteFavoriteMapper userRouteFavoriteMapper;

    @Autowired
    private RouteMapper routeMapper;

    @Autowired
    private PathMapper pathMapper;

    /**
     * 查询用户路线收藏
     * 
     * @param id 用户路线收藏主键
     * @return 用户路线收藏
     */
    @Override
    public UserRouteFavorite selectUserRouteFavoriteById(Long id)
    {
        UserRouteFavorite userRouteFavorite = userRouteFavoriteMapper.selectUserRouteFavoriteById(id);
        Route route = routeMapper.selectRouteById(userRouteFavorite.getRouteId());
        Path selectedPath = new Path();
        selectedPath.setRouteId(userRouteFavorite.getRouteId());
        List<Path> path = pathMapper.selectPathList(selectedPath);
        route.setPathList(path);
        userRouteFavorite.setRoute(route);
        return userRouteFavorite;
    }

    /**
     * 查询用户路线收藏列表
     * 
     * @param userRouteFavorite 用户路线收藏
     * @return 用户路线收藏
     */
    @Override
    public List<UserRouteFavorite> selectUserRouteFavoriteList(UserRouteFavorite userRouteFavorite)
    {
        List<UserRouteFavorite> userRouteFavoriteList = userRouteFavoriteMapper.selectUserRouteFavoriteList(userRouteFavorite);
        for (UserRouteFavorite item : userRouteFavoriteList) {
            Route route = routeMapper.selectRouteById(item.getRouteId());
            Path selectedPath = new Path();
            selectedPath.setRouteId(item.getRouteId());
            List<Path> path = pathMapper.selectPathList(selectedPath);
            route.setPathList(path);
            item.setRoute(routeMapper.selectRouteById(item.getRouteId()));
        }
        return userRouteFavoriteList;
    }

    /**
     * 新增用户路线收藏
     * 
     * @param userRouteFavorite 用户路线收藏
     * @return 结果
     */
    @Override
    public int insertUserRouteFavorite(UserRouteFavorite userRouteFavorite)
    {
        return userRouteFavoriteMapper.insertUserRouteFavorite(userRouteFavorite);
    }

    /**
     * 修改用户路线收藏
     * 
     * @param userRouteFavorite 用户路线收藏
     * @return 结果
     */
    @Override
    public int updateUserRouteFavorite(UserRouteFavorite userRouteFavorite)
    {
        return userRouteFavoriteMapper.updateUserRouteFavorite(userRouteFavorite);
    }

    /**
     * 批量删除用户路线收藏
     * 
     * @param ids 需要删除的用户路线收藏主键
     * @return 结果
     */
    @Override
    public int deleteUserRouteFavoriteByIds(Long[] ids)
    {
        return userRouteFavoriteMapper.deleteUserRouteFavoriteByIds(ids);
    }

    /**
     * 删除用户路线收藏信息
     * 
     * @param id 用户路线收藏主键
     * @return 结果
     */
    @Override
    public int deleteUserRouteFavoriteById(Long id)
    {
        return userRouteFavoriteMapper.deleteUserRouteFavoriteById(id);
    }

    @Override
    public int deleteUserRouteFavoriteByUserIdAndRouteId(Long userId, Long routeId) {
        return userRouteFavoriteMapper.deleteUserRouteFavoriteByUserIdAndRouteId(userId, routeId);
    }
}
